﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microserve.TeleEye.Common.Utility;
using System.Xml;
using System.Text;
using System.Net.Mail;
using System.Drawing;

namespace Microserve.TeleEye.Web
{
    public partial class DemoRegistration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtCaptcha.Text);
            if (ccJoin.UserValidated)
            {
                System.Net.NetworkCredential c = new System.Net.NetworkCredential();
                c.Password = "videocodec";
                c.UserName = "contact@teleeye.com.ph";
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = c;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.EnableSsl = true;
                smtp.Port = 587;
                smtp.Host = "smtp.gmail.com";

                if (this.txtEmail.Text.ToLower().Trim().IndexOf("mailinator.com") > 0)
                {
                    this.lblErrorMessage.ForeColor = Color.Red;
                    this.lblErrorMessage.Visible = true;
                    this.lblErrorMessage.Text = "Please enter a valid personal email address. Thank you.";

                    return;
                }                
                string toAddress = this.txtEmail.Text;
                string fromAddress = "lee.erwin@gmail.com";

                XmlDocument config = new XmlDocument();
                config.Load(PathHelper.GetSectionPath(HttpContext.Current, "emailsettings"));

                XmlNode from = config.SelectSingleNode("settings/client/from");
                if (from != null)
                {
                    fromAddress = from.InnerText;
                }

                System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress,
                    toAddress);

                XmlNode subject = config.SelectSingleNode("settings/client/subject");
                if (subject != null)
                {
                    mm.Subject = subject.InnerText;
                }

                XmlNode body = config.SelectSingleNode("settings/client/body");
                string username = string.Empty;
                string password = string.Empty;
                string address2 = string.Empty;
                string location = string.Empty;
                if (body != null)
                {
                    XmlNode demosite = null;
                    if (this.rdoHongkong.Checked)
                    {
                        demosite = config.SelectSingleNode("settings/demosites/demosite[@id='hongkong']");
                        location = "HongKong";
                    }
                    else if (this.rdoChina.Checked)
                    {
                        demosite = config.SelectSingleNode("settings/demosites/demosite[@id='china']");
                        location = "China";
                    }
                    else if (this.rdoUK.Checked)
                    {
                        demosite = config.SelectSingleNode("settings/demosites/demosite[@id='uk']");
                        location = "UK";
                    }
                    else if (this.rdoSingapore.Checked)
                    {
                        demosite = config.SelectSingleNode("settings/demosites/demosite[@id='singapore']");
                        location = "Singapore";
                    }
                    else if (this.rdoMx1.Checked)
                    {
                        demosite = config.SelectSingleNode("settings/demosites/demosite[@id='mx1']");
                        location = "mx1";
                    }
                    else if (this.rdoMx2.Checked)
                    {
                        demosite = config.SelectSingleNode("settings/demosites/demosite[@id='mx2']");
                        location = "mx3";
                    }
                    //else if (this.rdo.Checked)
                    //{
                    //    demosite = config.SelectSingleNode("settings/demosites/demosite[@id='mx3']");
                    //    location = "mx3";
                    //}

                    XmlNode xAddress = demosite.SelectSingleNode("address");
                    XmlNode xUsername = demosite.SelectSingleNode("username");
                    XmlNode xPassword = demosite.SelectSingleNode("password");

                    address2 = xAddress.InnerText;
                    username = xUsername.InnerText;
                    password = xPassword.InnerText;
                    mm.Body = string.Format(body.InnerXml, username, password, address2, location);
                }

                mm.IsBodyHtml = true;             

                bool isEmailSent = true;
                string exceptionMessage = string.Empty;


                try
                {
                    smtp.Send(mm);

                    this.lblErrorMessage.ForeColor = Color.Black;
                    this.lblErrorMessage.Text = "Thank you for registering. You will receive an email containing the demo information needed shortly.";
                    this.lblErrorMessage.Visible = true;
                    this.lnkOpenInNewWindow.Visible = true;
                    this.videodemo.Visible = true;
                    this.videodemo.Attributes["src"] = address2;

                    fields.Visible = false;

                }
                catch (Exception ex)
                {
                    this.lblErrorMessage.ForeColor = Color.Red;
                    this.lblErrorMessage.Visible = true;
                    this.lblErrorMessage.Text = "Problem occurred while trying to send your email, please click on  Register button to try again.";
                    isEmailSent = false;
                    exceptionMessage = ex.Message + "<br/>" + ex.StackTrace;
                    this.lblErrorMessage.Text += exceptionMessage;
                   // throw ex;
                }

                this.lnkOpenInNewWindow.HRef = address2;

                System.Net.Mail.MailMessage mm2 = new System.Net.Mail.MailMessage();
                mm2.From = new System.Net.Mail.MailAddress(fromAddress);

                XmlNodeList addresses = config.SelectNodes("settings/notify/addresses/address");

                foreach (XmlNode address in addresses)
                {
                   
                    mm2.To.Add(address.InnerText);                    
                }

                XmlNode copySubject = config.SelectSingleNode("settings/notify/subject");
                if (copySubject != null)
                {
                    mm2.Subject = string.Format(copySubject.InnerText, this.txtCompanyName.Text, this.txtName.Text);
                }

                XmlNode copyBody = config.SelectSingleNode("settings/notify/body");
                if (copyBody != null)
                {
                    mm2.Body = copyBody.InnerXml.Replace("{0}",
                        this.txtName.Text).Replace("{1}",
                        this.txtCompanyName.Text).Replace("{2}",
                        this.txtContactNumber.Text).Replace("{3}",
                        this.txtEmail.Text).Replace("{4}",
                        this.txtIndustry.Text);

                    mm2.Body += isEmailSent ? "<p> Demo information was successfully sent to the client. </p>" :
                        "<p>Demo information was not sent to the client because of an error that occurred." +
                        exceptionMessage + "</p>";
                }
                mm2.IsBodyHtml = true;

                try
                {
                    smtp.Send(mm2);
                }
                catch
                {
                }
            }
            else
            {
                this.lblErrorMessage.ForeColor = Color.Red;
                this.lblErrorMessage.Visible = true;
                this.lblErrorMessage.Text = "CAPTCHA Validation failed. Please reenter the word. Sorry for the inconvinience.";
            }

        }
    }
}
